<template>
  <view class="login">
    <view class="logo-name">
      <u-avatar size="194rpx" :src="appLogo"></u-avatar>
      <view class="name">{{ appName }}</view>
    </view>
    <view>
      <block v-if="miniapp_auth === 1">
        <button
            @getphonenumber="getPhoneNumber"
            class="login-btn"
            openType="getPhoneNumber"
            :plain="true"
            v-if="agreeChecked"
        >
          一键授权登录
        </button>
        <button @tap="checkAgree" class="login-btn" :plain="true" v-else>一键授权登录
        </button>
        <view @tap="goMobileLogin" class="other-mobile-login">
          其他手机号登录
        </view>
      </block>
      <button @tap="goMobileLogin" class="login-btn" v-else>
        手机号登录
      </button>
      <view class="agreement">
        <u-checkbox-group
            placement="column"
            shape="circle"
            v-model="checkedList"
        >
          <u-checkbox label="我已阅读并同意" labelSize="26rpx" activeColor="#ea4679" name="1"></u-checkbox>
        </u-checkbox-group>
        <text @tap="goArticle('service')" class="link">《服务协议》</text>
        <text>和</text>
        <text @tap="goArticle('privacy')" class="link">《隐私政策》</text>
      </view>
    </view>
    <setUserAvatar ref="loginSetUserInfoRef"></setUserAvatar>
  </view>
</template>

<script>
import setUserAvatar from '@/components/setUserAvatar'
export default {
  components: { setUserAvatar },
  data() {
    return {
      appLogo: '',
      appName: '',
      password: true,
      checkedList: [],
      miniapp_auth: 1,
      isUser: 0,
      originToken: '',
      protocolConfig: null
    };
  },
  computed: {
    agreeChecked() {
      return this.checkedList.length > 0
    }
  },
  onLoad() {

  },
  methods: {
    goMobileLogin() {
      if (!this.agreeChecked) {
        return this.$util.message('请勾选用户协议和隐私政策');
      }
      uni.navigateTo({
        url: '/pages/login/login-code'
      });
    },
    getPhoneNumber(e) {
      var that = this;
      var code = e.detail.code
      if (!code) {
        this.$util.message('获取手机号失败，请手动输入', 'error', function () {
          that.goMobileLogin()
        })
        return;
      }
      const tuid = uni.getStorageSync('tuid')
      this.$util.request({
        url: '/login/loginWithWxappMobile',
        data: {
          code: code,
          tuid: tuid ? tuid : 0
        }
      }).then(res => {
        uni.setStorageSync('token', res.data.token);
        if (res.data.is_new === 1 || !res.data.avatar || res.data.avatar.indexOf('wxfile://') !== -1) {
          this.$refs.loginSetUserInfoRef.userInfoShow = true;
        } else {
          this.$util.message(res.message, 'success')
          setTimeout(function () {
            uni.reLaunch({
              url: '/pages/my/my'
            })
          }, 1000);
        }
      });
    },
    checkAgree() {
      if (!this.checked) {
        this.$util.message('请勾选用户协议和隐私政策');
        return false
      }
    },
    goArticle(type) {
      uni.navigateTo({
        url: '/pages/article/detail?type=' + type
      });
    }
  }
}
</script>
<style scoped>
@import './login.css';
</style>
